package com.gidgabs.common.utils;

import com.gidgabs.utils.JSONUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

/**
 * 地图工具类
 *
 * @Auther: LanYongLin
 * @Describotion:
 * @Date: 上午 10:11 2018/5/24 0024
 */
public class MapUtil {



//    服务地址-驾车
    public static String driving = "http://api.map.baidu.com/routematrix/v2/driving?";
//    https://api.map.baidu.com/routematrix/v2/driving?
//    服务地址-骑行
    public static String riding = "http://api.map.baidu.com/routematrix/v2/riding?";
//    https://api.map.baidu.com/routematrix/v2/riding?
//    服务地址-步行
//    http://api.map.baidu.com/routematrix/v2/walking?
    public static String walking = "http://api.map.baidu.com/routematrix/v2/walking?";
//    https://api.map.baidu.com/routematrix/v2/walking?
//    示例URL
//    http://api.map.baidu.com/routematrix/v2/driving?output=json&origins=40.45,116.34|40.54,116.35&destinations=40.34,116.45|40.35,116.46&ak=您的ak
    public static String ak = "L3HbdV2gCqSOWKUnFZsAZp5QMtGFLWYA";



    public String sendGetRequest(String url) {
        StringBuffer stringBuffer = new StringBuffer("");
        try {
            URL postUrl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("GET");
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                stringBuffer.append(line);
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stringBuffer.toString();
    }

    /**
     * 根据两个位置的经纬度计算距离
     *
     * @param originslng 当前位置经度
     * @param originslat 当前位置纬度
     * @param destinationslng 目的地位置经度
     * @param destinationslat 目的地位置纬度
     * @param urlType 过去类型
     * @return
     */
    public String getDistance(String originslng,String originslat,String destinationslng,String destinationslat,String urlType){
        String url = urlType+"output=json&origins="+originslat+","+originslng+
                "&destinations="+destinationslat+","+destinationslng+"&ak="+ak;
        String s = new MapUtil().sendGetRequest(url);
        System.out.println(s);
        Map ss =JSONUtils.parseJSON2Map(s);
        Object ssd = ss.get("result");
        List<Map<String, Object>>  qwe = JSONUtils.parseJSON2List(ssd.toString());
        Map<String, Object> sss =JSONUtils.parseJSON2Map(qwe.get(0).get("distance").toString());
        return sss.get("text").toString();
    }

    public static void main(String[] a){
        String originslng = "104.067";
        String originslat = "30.679";
        String destinationslng = "106.545";
        String destinationslat = "29.502";

        String text = new MapUtil().getDistance(originslng,originslat,destinationslng,destinationslat,driving);
        System.out.println(text);
    }
}
